//index.js
const app = getApp()
const {
  envList
} = require('../../envList.js')

var ip = "https://0.0.0.0/sr"

Page({
  data: {
    showUploadTip: false,
    envList,
    selectedEnv: envList[0],
    haveCreateCollection: false
  },

  //上传图片开始
  chooseImg: function (e) {
    var that = this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        for (var i = 0; i < tempFilePaths.length; i++) {
          // pics.push(tempFilePaths[i]);
          wx.uploadFile({
            filePath: tempFilePaths[i],
            name: 'file',
            url: ip,
            success: (resp) => {
              var str = resp.data
              console.log(resp)
              var image = 'data:image/png;base64,' + str
              that.setData({
                image: image,
                str: str,
                show: true
              })
              wx.hideToast({
                success: (res) => {},
              })
            },
            fail: function (res) {
              console.log(res)
              wx.hideToast({
                success: (res) => {},
              })
              wx.showToast({
                title: '上传失败',
                icon: 'none',
                duration: 1500, //提示的延迟时间，单位毫秒，默认：1500
            })
            }
          })
        }

      },
    });
  },

  //保存图片
  saveImg: function (e) {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
            }
          })
        }
      }
    })
    console.log('授权成功')
    var save = wx.getFileSystemManager();
    var number = Math.random();
    var path = wx.env.USER_DATA_PATH + '/pic' + number + '.png'
    var that = this
    save.writeFile({
        filePath: path,
        data: that.data["str"],
        encoding: 'base64',
        success: res => {
            wx.saveImageToPhotosAlbum({ //保存为png格式到相册
                filePath: path,
                success: function (res) {
                    wx.showToast({
                        title: '保存成功',
                        icon: 'none',
                        duration: 1500, //提示的延迟时间，单位毫秒，默认：1500
                    })
                },
                fail: function (err) {
                    console.log(err)
                    wx.showToast({
                      title: '保存失败',
                      icon: 'none',
                      duration: 1500, //提示的延迟时间，单位毫秒，默认：1500
                  })
                }
            })
        }, 
        fail: err => {
            console.log(err)
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 1500, //提示的延迟时间，单位毫秒，默认：1500
          })
        }
    })
  },
})